<template>
	<view class="container">
		<userinfo v-if="user_options.toLogin" :options="user_options" @getUserscope="getUserInfo" @toLogin="getUserInfo"></userinfo>
		<view class="top">
			<image src="../../../static/images/money1.png" mode="" class="top-img"></image>
			<view class="text">立减停车费{{money}}元</view>
		</view>
		<view class="select-plate" v-if="listdata[0]">
			快速选择车牌
		</view>
		<view class="plate-box" v-if="listdata[0]">
			<view class="plate" @tap="selectPlate(item.car_number)" v-for="(item,index) in listdata">{{item.car_number}}</view>
		</view>
		<keyboard @keyboard="keyboardChange" ref="plate" :plateNum="plateNum" :show.sync="show" active-border="#0deafe"
		 base-border="38f8f8f"></keyboard>
		 <view class="buttom">
			 <image src="../../../static/images/zhuyi.png" mode="" class="zhuyi"></image>
			 <view class="text1">请仔细核对车牌号,谨防输入错误</view>
		 </view>
		<button type="primary" :style="btn_style" class="btn" @tap="uPublishTap">立即领取</button>
	</view>
</template>

<script>
	import keyboard from "../component/mpvue-keyboard/mpvue-keyboard.vue";
	var common = require('../../../utils/common.js');
	var app = getApp();
	var _this;
	const getparkinginfo = () => {
		common.post('HouseApi&a=park_coupon_info', {
			village_id: _this.village_id,
		}, data => {
			console.log('++++', data)
			_this.money = data.receive_coupon_fee;
			_this.listdata = data.dataList;
		});
	};
	const getparkadd = () => {
		common.post('HouseApi&a=park_coupon_add', {
			village_id: _this.village_id,
			car_number: _this.car_number,
		}, data => {
			console.log('++++', data)
			
			// uni.navigateTo({
			// 	url:'/pages/village/smartPark/parkingFeeDetails'
			// })
			
		});
	};
	export default {
		data() {
			return {
				listdata: {},
				numlist: {},
				user_options: {},
				village_id: 0,
				pigcms_id: 0,
				actcolor: '',
				btn_style: '',
				show: true,
				plateNum: '',
				money:'0',
				car_number:'',
			}
		},
		onShow() {
			// #ifdef H5
			common.hideWxShare()
			// #endif
		},
		onLoad(options) {
			_this = this;
			_this.village_id = options.village_id;
			// #ifdef H5
			console.log('准备跳小程序页', _this.id);
			common.webToMiniapp(_this.route, options, 'redirect')
			// #endif

			common.setMainColor(_this, function(config) {
				_this.actcolor = config.config.mobile_head_color
				_this.btn_style = 'background-color: ' + _this.actcolor + ';box-shadow:0px 30rpx 30rpx -30rpx ' + _this.actcolor +
					';';

				const res = uni.getStorageSync('village_key');
				if (res) {
					_this.village_id = res.village_id;
					_this.pigcms_id = res.pigcms_id;
					_this.village_name = res.village_name;
				};
				if(app.globalData.is_login){
					getparkinginfo()
				}else{
					_this.user_options = {
						toLogin: true
					};
				}
			});
		},
		methods: {
			keyboardChange(e) {
				uni.showToast({
					icon: "none",
					title: e,
					mask: true,
					duration: 2000
				});
				_this.car_number = e;
			},
			selectPlate(car_number) {
				_this.car_number = car_number;
				var number = car_number.split("");
				if(number.length > 7){
					_this.$refs['plate'].isEnergy = false;
					_this.input_box(car_number)
				}else{
					_this.input_box(car_number)
				}
			},
			input_box(car_number){
				var number = car_number.split("");
				for(let i in number){
					_this.$refs['plate'].tapVal = number[i];
					console.log("删除",_this.$refs['plate'].tapVal)
					if (_this.$refs['plate'].tapVal === '-') {
					    _this.$refs['plate'].textArr.pop();
					    _this.$refs['plate'].textBaseArr.splice(_this.$refs['plate'].textArr.length, 1, '');
					    if (
					        _this.$refs['plate'].textArr.length === 7 &&
					        !_this.$refs['plate'].isEnergy &&
					        _this.$refs['plate'].tapVal !== ''
					    ) {
					        _this.$refs['plate'].isEnergy = true;
					        _this.$refs['plate'].tapVal = '';
					    }
					} else {
					    if (_this.$refs['plate'].textArr.length > 7) return false;
					    if (_this.$refs['plate'].textArr.length > 6 && _this.$refs['plate'].isEnergy) {
					        _this.$refs['plate'].textBaseArr.splice(6, 1, _this.$refs['plate'].tapVal);
					        _this.$refs['plate'].textArr.splice(6, 1, _this.$refs['plate'].tapVal);
					        return false;
					    }
					    _this.$refs['plate'].textArr = [..._this.$refs['plate'].textArr, _this.$refs['plate'].tapVal];
					    _this.$refs['plate'].textBaseArr.splice(
					        _this.$refs['plate'].textArr.length - 1,
					        1,
					        _this.$refs['plate'].tapVal
					    );
					}
					
					if (_this.$refs['plate'].textArr.length === 1) {
					    _this.$refs['plate'].isAlph = true;
					    _this.$refs['plate'].isNum = false;
					} else if (_this.$refs['plate'].textArr.length === 0) {
					    _this.$refs['plate'].isAlph = false;
					    _this.$refs['plate'].isNum = false;
					} else {
					    _this.$refs['plate'].isNum = true;
					}
					console.log("_this.car_number",_this.car_number)
				}
			},
			uPublishTap(){
				_this.car_number = _this.$refs['plate'].textArr.join('')
				getparkadd()
			}
		},
		components: {
			keyboard
		}
	}
</script>

<style>
	page {
		background: #FFFFFF;
	}

	.container {
		margin: 0;
		padding: 0;
		height: 100%;
	}
	.top{
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 15rpx;
		padding: 0rpx 30rpx;
		background: rgba(255,192,203,0.1);
	}
	.top-img {
		width: 35rpx;
		height: 35rpx;
		display: block;
		margin-right: 15rpx;
	}
	.text {
		font-size: 28rpx;
		color: red;
		line-height: 60rpx;
	}
	.select-plate {
		margin: 30rpx 30rpx 15rpx 30rpx;
		font-size: 32rpx;
		font-weight: 700;
	}

	.plate-box {
		margin: 15rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		padding-bottom: 15rpx;
		border-bottom: 2rpx solid #F1F1F1;
	}

	.plate {
		padding: 2rpx 10rpx;
		background: #F1F1F1;
		color: rgb(160, 160, 160);
		margin-left: 15rpx;
		margin-top: 15rpx;
	}
	.buttom {
		margin: 150rpx 15rpx 0rpx 15rpx;
		border-top: 2rpx solid #F1F1F1;
		padding-top: 15rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.zhuyi{
		width: 25rpx;
		height: 25rpx;
		display: inline-block;
		margin-right: 15rpx;
	}
	.text1 {
		font-size: 24rpx;
	}
	.btn {
		margin: 150rpx 50rpx 150rpx 50rpx;
		border-radius: 50rpx;
	}
</style>
